package com.bmob.lover.app.util

import android.text.TextUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.qmuiteam.qmui.util.QMUIDeviceHelper
import com.theone.common.ext.formatString
import com.bmob.lover.app.ext.toJson
import com.bmob.lover.app.ext.toJsonWithoutAttr
import com.bmob.lover.data.model.bean.FestivalBg
import com.bmob.lover.data.model.bean.LoverBind
import com.bmob.lover.data.model.bean.User
import com.bmob.lover.data.model.bean.UserLocation
import com.theone.mvvm.core.app.util.MMKVUtil
import java.util.*
import kotlin.collections.ArrayList


//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 * @author The one
 * @date 2021/3/11 0011
 * @describe TODO
 * @email 625805189@qq.com
 * @remark
 */

fun getCurrentUser(): User? = CacheUtil.getUser()

object CacheUtil {

    private const val SESSION_TOKEN: String = "session_token"
    private const val HISTORY: String = "search_history"
    private const val ANIMATION: String = "animation_type"
    private const val FIRST: String = "app_first"
    private const val USER: String = "user"
    private const val LOVER_BIND: String = "lover_bind"
    private const val USER_LOCATION: String = "user_location"
    private const val FESTIVAL_BG: String = "festival_bg"
    private const val DELETE_FILE: String = "delete_file"


    fun setUser(userInfo: User?) {
        val user = if (null != userInfo) {
            userInfo.sessionToken?.let {
                setSessionToken(it)
            }
            Gson().toJson(userInfo)
        } else {
            setSessionToken(null)
            ""
        }
        MMKVUtil.putString(USER, user)
    }

    fun getUser(): User? {
        val json = MMKVUtil.getString(USER)
        return if (json.isNullOrEmpty())
            null
        else
            Gson().fromJson(json, User::class.java)
    }

    private fun setSessionToken(token: String?) {
        MMKVUtil.putString(SESSION_TOKEN, token)
    }

    fun getSessionToken(): String? = MMKVUtil.getString(SESSION_TOKEN)

    fun setLoverBind(bean: LoverBind?) {
        val user = if (null != bean) Gson().toJson(bean) else ""
        MMKVUtil.putString(LOVER_BIND, user)
    }

    fun getLoverBind(): LoverBind? {
        val json = MMKVUtil.getString(LOVER_BIND)
        return if (json.isNullOrEmpty())
            null
        else
            Gson().fromJson(json, LoverBind::class.java)
    }

    fun getLover(): User? {
       return getLoverBind()?.getBindLover(getCurrentUser())
    }

    fun setUserLocation(location: UserLocation?) {
        location?.let {
            // 保存的时候这个字段是为空的，为了使用方便，这个直接将类型指向Pointer
            it.__type = "Pointer"
        }
        // user 字段里的不保存
        val json = location.toJsonWithoutAttr("user")
        MMKVUtil.putString(USER_LOCATION, json)
    }

    fun getUserLocation(): UserLocation? {
        val location = MMKVUtil.getString(USER_LOCATION)
        return if (location.isNullOrEmpty())
            null
        else
            Gson().fromJson(location, UserLocation::class.java)
    }

    fun isFirst(): Boolean = MMKVUtil.getBoolean(FIRST, true)

    fun isEnterApp() {
        MMKVUtil.putBoolean(FIRST, false)
    }

    /**
     * 文件上传成功后是否删除源文件
     * @return Boolean
     */
    fun isUploadSuccessDeleteFile() = MMKVUtil.getBoolean(DELETE_FILE, false)

    fun setUploadSuccessDeleteFile(delete:Boolean){
        MMKVUtil.putBoolean(DELETE_FILE,delete)
    }

    /**
     * 以日期为
     * @param date String
     * @param bg String
     */
    fun setFestivalBg(bg: String, date: String = Date().formatString(), isNew: Boolean) {
        MMKVUtil.putString(FESTIVAL_BG, FestivalBg(date, bg, isNew).toJson())
    }

    /**
     * 获取
     * @return FestivalBg?
     */
    fun getFestivalBg(): FestivalBg? {
        val festivalBg = MMKVUtil.getString(FESTIVAL_BG)
        return if (festivalBg.isNullOrEmpty())
            null
        else
            Gson().fromJson(festivalBg, FestivalBg::class.java)
    }

    /**
     * 获取搜索历史缓存数据
     */
    fun getSearchHistoryData(): ArrayList<String> {
        val searchCacheStr = MMKVUtil.getString(HISTORY)
        if (!TextUtils.isEmpty(searchCacheStr)) {
            return Gson().fromJson(
                searchCacheStr
                , object : TypeToken<ArrayList<String>>() {}.type
            )
        }
        return arrayListOf()
    }

    /**
     * 设置搜索历史数据
     */
    fun setSearchHistoryData(searchResponseStr: String) {
        MMKVUtil.putString(HISTORY, searchResponseStr)
    }

    /**
     * 获取列表动画类型
     */
    fun getAnimationType(): Int = MMKVUtil.getInt(ANIMATION, 0)

    /**
     * 设置类表动画类型
     */
    fun setAnimationType(type: Int) {
        MMKVUtil.putInt(ANIMATION, type)
    }


}